﻿using System;
using System.Collections.Generic;

namespace IntelligentEscalator.DLL
{
    public class Singleton<T> where T : class
    {
        private static readonly Dictionary<string, T> _instances = new Dictionary<string, T>();
        private static readonly object _lockObject = new object();

        public static T GetInstance(params object[] args)
        {
            string key = GetKey(args);
            if (!_instances.ContainsKey(key))
            {
                lock (_lockObject)
                {
                    if (!_instances.ContainsKey(key))
                    {
                        _instances[key] = (T)Activator.CreateInstance(typeof(T), args);
                    }
                }
            }
            return _instances[key];
        }

        private static string GetKey(object[] args)
        {
            string key = "";
            foreach (var arg in args)
            {
                key += arg.ToString();
            }
            return key;
        }
    }
}
